import axios from 'axios'

const customAxios = axios.create({
    baseURL: 'http://localhost:3000/api/v1/',
    timeout: 1000,
});

// 请求拦截器
customAxios.interceptors.request.use(
    config => {
        config.headers = config.headers || {};
        const token = localStorage.getItem('authToken');
        if(token){
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    error => {
        return Promise.reject('请求出错：'+error);
    }
)

// 响应拦截器
customAxios.interceptors.response.use(
    response => {
        // 对响应数据做点什么
        return response.data;
    },
    error => {
        console.error(`请求出错：${error.message}`);
        return Promise.reject(error);
    }
)

export default customAxios;